﻿Imports System.Data.SqlClient
Imports System.Data
Public Class Paciente
#Region "Miembros"
    Dim con As Conexion
    Dim _primerNombre As String
    Dim _primerApellido As String
    Dim _segundoNombre As String
    Dim _segundoApellido As String
    Dim _telefono As String
    Dim _ocupacion As String
    Dim _email As String
    Dim id As Int32

#End Region
#Region "propiedades"
    Public ReadOnly Property NombreCompleto() As String
        Get
            Return _primerNombre & " " & _primerApellido
        End Get
    End Property

    Public ReadOnly Property PrimerNombre() As String
        Get
            Return Me._primerNombre
        End Get
    End Property

    Public ReadOnly Property primerApellido() As String
        Get
            Return Me._primerApellido
        End Get
    End Property

    Public ReadOnly Property segundoNombre() As String
        Get
            Return Me._segundoNombre
        End Get
    End Property
    Public ReadOnly Property segundoApellido() As String
        Get
            Return Me._segundoApellido
        End Get
    End Property

    Public ReadOnly Property telefono() As String
        Get
            Return Me._telefono
        End Get
    End Property

    Public ReadOnly Property ocupacion() As String
        Get
            Return Me._ocupacion
        End Get
    End Property

    Public ReadOnly Property email() As String
        Get
            Return Me._email
        End Get
    End Property

    Public ReadOnly Property Id_cliente() As Int32
        Get
            Return Me.id
        End Get
    End Property
#End Region

    Public Sub New(ByVal primerNombre As String, ByVal primerApellido As String, Optional ByVal segundoNombre As String = Nothing, _
                               Optional ByVal segundoApellido As String = Nothing, _
                              Optional ByVal telefono As String = Nothing, Optional ByVal email As String = Nothing, _
                              Optional ByVal ocupacion As String = Nothing)

        Me._primerNombre = primerNombre
        Me._primerApellido = primerApellido
        Me._segundoApellido = segundoApellido
        Me._segundoNombre = segundoNombre
        Me._telefono = telefono
        Me._email = email
        Me._ocupacion = ocupacion

        Dim comando As SqlCommand
        con = New Conexion
        con.Conectar()

        comando = New SqlCommand("crear_paciente", con.SqlCon)
        comando.CommandType = CommandType.StoredProcedure

        comando.Parameters.AddWithValue("@primer_nombre", Me.PrimerNombre)
        comando.Parameters.AddWithValue("@segundo_nombre", Me.segundoNombre)
        comando.Parameters.AddWithValue("@primer_apellido", Me.primerApellido)
        comando.Parameters.AddWithValue("@segundo_apellido", Me.segundoApellido)
        comando.Parameters.AddWithValue("@telefono", Me.telefono)
        comando.Parameters.AddWithValue("@ocupacion", Me.ocupacion)
        comando.Parameters.AddWithValue("@email", Me.email)

        Try
            comando.ExecuteNonQuery()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

        con.Desconectar()
    End Sub

    Public Sub New(ByVal id As Int32)
        Me.id = id
        con = New Conexion
        con.Conectar()
        Dim comando As New SqlCommand("seleccionar_paciente", con.SqlCon)
        comando.CommandType = CommandType.StoredProcedure
        comando.Parameters.AddWithValue("@id", id)
        Dim da As New SqlDataAdapter(comando)
        Dim ds As New DataSet
        da.Fill(ds)
        con.Desconectar()

        If ds.Tables(0).Rows.Count > 0 Then
            Me._primerNombre = ds.Tables(0).Rows(0).Item(0).ToString
            Me._segundoNombre = ds.Tables(0).Rows(0).Item(1).ToString
            Me._primerApellido = ds.Tables(0).Rows(0).Item(2).ToString
            Me._segundoApellido = ds.Tables(0).Rows(0).Item(3).ToString
            Me._telefono = ds.Tables(0).Rows(0).Item(4).ToString
            Me._email = ds.Tables(0).Rows(0).Item(5).ToString
            Me._ocupacion = ds.Tables(0).Rows(0).Item(6).ToString
        Else
            MsgBox("Paciente No Encontrado", MsgBoxStyle.Critical, "Error")
        End If
        da.Dispose()
        ds.Dispose()
    End Sub

    Public Sub modificar_paciente(ByVal primerNombre As String, ByVal primerApellido As String, Optional ByVal segundoNombre As String = Nothing, _
                               Optional ByVal segundoApellido As String = Nothing, _
                              Optional ByVal telefono As String = Nothing, Optional ByVal email As String = Nothing, _
                              Optional ByVal ocupacion As String = Nothing)
        con = New Conexion
        con.Conectar()

        Dim comando As New SqlCommand("modificar_paciente", con.SqlCon)
        comando.CommandType = CommandType.StoredProcedure

        comando.Parameters.AddWithValue("@primer_nombre", primerNombre)
        comando.Parameters.AddWithValue("@segundo_nombre", segundoNombre)
        comando.Parameters.AddWithValue("@primer_apellido", primerApellido)
        comando.Parameters.AddWithValue("@segundo_apellido", segundoApellido)
        comando.Parameters.AddWithValue("@telefono", telefono)
        comando.Parameters.AddWithValue("@ocupacion", ocupacion)
        comando.Parameters.AddWithValue("@email", email)
        comando.Parameters.AddWithValue("@id", Me.id)
        Try
            comando.ExecuteNonQuery()
            MsgBox("Usuario modificado", MsgBoxStyle.Information)
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical, "Error!")
            con.Desconectar()
        End Try
        con.Desconectar()

    End Sub

    Public Sub eliminar()
        con = New Conexion
        con.Conectar()

        Dim comando As New SqlCommand("eliminar_paciente", con.SqlCon)
        comando.CommandType = CommandType.StoredProcedure

        comando.Parameters.AddWithValue("@id", Me.id)
        comando.ExecuteNonQuery()

        con.Desconectar()
    End Sub

End Class
